﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL = AccessDAL;
using CommonFunc;

public partial class Manageweb_ManageProduct_ManageProductClass : System.Web.UI.Page
{
    DAL.ProductClassTableAdapters.ProductClassTableAdapter DPC =
        new DAL.ProductClassTableAdapters.ProductClassTableAdapter();

    MODEL.ProductClass MPC = new MODEL.ProductClass();
    MODEL.ProductClass _MPC = new MODEL.ProductClass();

    BLL.ProductClass BPC = new BLL.ProductClass();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["MasterID"] == null)
        {
            Response.Redirect("../Default.aspx");
        }

        LB_ManageProductClass.Text = "";
        
        TABLE_Add_Mod.Visible = false;

        ClickInit();

        if (!IsPostBack)
        {
            DDL_ClassDataBind();
            PageDataInit();
        }
    }


    #region ClickInit [初始化]
    protected void ClickInit()
    {
        LB_ManageProductClass.Text = "";

        try
        {
            if (LB_ID.Text == "")
            {
                Btn_Submit.Click += new EventHandler(Btn_Add_Click);
                LB_TableTitle.Text = "添加类别";
            }
            else
            {
                Btn_Submit.Click += new EventHandler(Btn_Mod_Click);
                LB_TableTitle.Text = "修改类别";
            }
        }
        catch (Exception ex)
        {
            LB_ManageProductClass.Text = ex.Message;
        }
    }
    #endregion


    #region 页面数据 [初始化]
    public void PageDataInit()
    {
        try
        {
            if (LB_ID.Text == "")
            {
                TB_Title.Text = "";

                DDL_Class.SelectedValue = "";

                DDL_Class.Enabled = true;
            }
            else
            {
                MPC.PC_ID = Convert.ToInt32(LB_ID.Text);

                _MPC = BPC.GetDataByID(MPC);

                TB_Title.Text = _MPC.PC_Title;

                DDL_Class.SelectedValue = _MPC.PC_ParentID.ToString();

                DDL_Class.Enabled = false;
            }
        }
        catch (Exception ex)
        {
            LB_ManageProductClass.Text = ex.Message;
        }
    }
    #endregion


    #region DDL_Class数据绑定 [初始化]
    protected void DDL_ClassDataBind()
    {
        try
        {
            DDL_Class.Items.Clear();

            ListItem LI0 = new ListItem("", "");
            DDL_Class.Items.Add(LI0);
            ListItem LI1 = new ListItem("根目录", "0");
            DDL_Class.Items.Add(LI1);

            DataTable DT = DPC.GetDataOrderByClassCode(false);

            foreach (DataRow dr in DT.Rows)
            {
                ListItem li = new ListItem(
                    ClassCode.CreateClassListItemText(dr["PC_Title"].ToString(), dr["PC_Level"].ToString()),
                    dr["PC_ID"].ToString());

                DDL_Class.Items.Add(li);
            }
        }
        catch (Exception ex)
        {
            LB_ManageProductClass.Text = ex.Message;
        }
    }
    #endregion


    #region 类别标题 [格式化]
    protected string FormatClassTitle(object _Title, object _Level)
    {
        return ClassCode.CreateClassListItemText(_Title.ToString(), _Level.ToString());
    }
    #endregion


    #region 添加类别 [Command] [GridView]
    protected void LBtn_Add_Command(object sender, CommandEventArgs e)
    {
        try
        {
            TABLE_Add_Mod.Visible = true;

            LB_ID.Text = "";

            PageDataInit();
        }
        catch (Exception ex)
        {
            LB_ManageProductClass.Text = ex.Message;
        }
    }
    #endregion
    #region 修改类别 [Command] [GridView]
    protected void LBtn_Mod_Command(object sender, CommandEventArgs e)
    {
        try
        {
            TABLE_Add_Mod.Visible = true;

            LB_ID.Text = e.CommandArgument.ToString();

            PageDataInit();
        }
        catch (Exception ex)
        {
            LB_ManageProductClass.Text = ex.Message;
        }
    }
    #endregion
    #region 删除类别 [Command] [GridView]
    protected void LBtn_Del_Command(object sender, CommandEventArgs e)
    {
        try
        {
            MPC.PC_ID = Convert.ToInt32(e.CommandArgument);

            BPC.Del(MPC);

            GV_ProductClassList.DataBind();

            DDL_ClassDataBind();

            LB_ManageProductClass.Text = "删除成功";
        }
        catch (Exception ex)
        {
            LB_ManageProductClass.Text = ex.Message;
        }
    }
    #endregion


    #region 添加类别 [Click]
    protected void Btn_Add_Click(object sender, EventArgs e)
    {
        try
        {
            MPC.PC_Title = TB_Title.Text;
            MPC.PC_ID = Convert.ToInt32(DDL_Class.SelectedValue);
            MPC.PC_Thumb = "";
            MPC.PC_IsLock = false;

            //锁定一级分类
            //if (DDL_Class.SelectedValue != "0")
            //{
            //    throw new Exception("目前只能添加一级分类");
            //}
            //锁定二级以上分类
            //if (DDL_Class.SelectedValue != "0")
            //{
            //    _MPC = BPC.GetDataByID(MPC);
            //    if (_MPC.PC_Level > 1)
            //    {
            //        throw new Exception("目前只能添加二级分类");
            //    }
            //}
            

            BPC.Add(MPC);

            LB_ManageProductClass.Text = "添加成功";

            GV_ProductClassList.DataBind();

            DDL_ClassDataBind();
        }
        catch (Exception ex)
        {
            LB_ManageProductClass.Text = ex.Message;
        }
    }
    #endregion


    #region 修改类别 [Click]
    protected void Btn_Mod_Click(object sender, EventArgs e)
    {
        try
        {
            MPC.PC_Title = TB_Title.Text;
            MPC.PC_ID = Convert.ToInt32(LB_ID.Text);

            BPC.Mod(MPC);

            LB_ManageProductClass.Text = "修改成功";

            GV_ProductClassList.DataBind();

            DDL_ClassDataBind();
        }
        catch (Exception ex)
        {
            LB_ManageProductClass.Text = ex.Message;
        }
    }
    #endregion
}
